﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ExamManagementSystem
{
    public partial class AddTeacherForm : Form
    {
        public AddTeacherForm()
        {
            InitializeComponent();
        }

        private void AddTeacherForm_Load(object sender, EventArgs e)
        {

        }
        //单击“添加”按钮时，向数据库添加教师信息
        private void btnAdd_Click(object sender, EventArgs e)
        {
            String LoginId = this.txtLoginId.Text.ToString();
            String LoginPwd = this.txtLoginPwd.Text.ToString();
            String Name = this.txtName.Text.ToString();

            //添加信息判断
            if (LoginId == null || LoginPwd == null || Name == null)
                MessageBox.Show("请填写完整！");
            else
            {
                SqlConnection conn = DBHelper.connection;
                conn.Open();
                String strSql="select count(*) from Teacher where LoginId='"+LoginId+"'";
                SqlCommand cmdCheck = new SqlCommand(strSql,conn);
                int iResult = Convert.ToInt32(cmdCheck.ExecuteScalar());
                conn.Close();
                if (iResult == 0)
                {
                    conn.Open();
                    String strSqlAdd = "insert into Teacher values('" + LoginId + "','" + LoginPwd + "','" + Name + "')";
                    SqlCommand cmdAdd = new SqlCommand(strSqlAdd, conn);
                    if ((int)cmdAdd.ExecuteNonQuery()> 0)
                    {
                        MessageBox.Show("添加成功！");
                        this.txtLoginId.Text = "";
                        this.txtLoginPwd.Text = "";
                        this.txtName.Text = "";
                    }
                    conn.Close();
                }
                else
                {
                    MessageBox.Show("登录名已存在!", "试题管理系统(V1.0)", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtLoginId.Text = "";
                    this.txtLoginPwd.Text = "";
                    this.txtName.Text = "";
                }

            }

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtLoginId.Text = "";
            this.txtLoginPwd.Text = "";
            this.txtName.Text = "";
        }
    }
}
